package com.seek.service.impl;

import com.seek.domain.SysUser;
import com.seek.service.SysUserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * @author : seek
 * @version : v1.0
 * @date : 2018-09-22
 */
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getUserByUsername(username);
        if(sysUser == null){
            throw new UsernameNotFoundException("username:" + username + " does not exist!");
        }
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
//        grantedAuthorityList.add(new GrantedAuthorityImpl("MY_ROLE1", "MY_MENU1"));
//        grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER"));
//        grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        //获取查询到的用户和密码给user,user会自动比对和输入的密码是否一致
        return new org.springframework.security.core.userdetails.User(sysUser.getUsername(), "{noop}"+sysUser.getPassword(), grantedAuthorityList);
    }
}
